package com.cq.hd.common.utils;

import lombok.Data;

import java.util.List;

/**
 * @description: 对list集合分页封装
 * @author: mYunYu
 * @date: 2021/7/7 16:20
 * @version: v1.0
 */
@Data
public class ListPageUtil<T> {

    /**
     * 原集合
     */
    private List<T> data;

    /**
     * 上一页
     */
    private int lastPage;

    /**
     * 当前页
     */
    private int nowPage;

    /**
     * 下一页
     */
    private int nextPage;

    /**
     * 每页条数
     */
    private int pageSize;

    /**
     * 总页数
     */
    private int totalPage;

    /**
     * 总数据条数
     */
    private int totalCount;

    public ListPageUtil(List<T> data, int nowPage, int pageSize) {
        if (data == null || data.isEmpty()) {
            throw new IllegalArgumentException("data must be not empty!");
        }

        this.data = data;
        this.pageSize = pageSize;
        this.nowPage = nowPage;
        this.totalCount = data.size();
        this.totalPage = (totalCount + pageSize - 1) / pageSize;
        this.lastPage = Math.max(nowPage - 1, 1);
        this.nextPage = nowPage >= totalPage ? totalPage : nowPage + 1;

    }

    /**
     * @Description: 得到分页后的数据
     * @Param: []
     * @Return: java.util.List<T>
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:20
     */
    public List<T> getPagedList() {
        int fromIndex = (nowPage - 1) * pageSize;
        if (fromIndex >= data.size()) {
            return null;//空数组
        }
        if (fromIndex < 0) {
            return null;//空数组
        }
        int toIndex = nowPage * pageSize;
        if (toIndex >= data.size()) {
            toIndex = data.size();
        }
        return data.subList(fromIndex, toIndex);
    }


}
